package cn.itsource.pethome.dept.service.impl;

import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.base.util.ResultBean;
import cn.itsource.pethome.dept.domain.Department;
import cn.itsource.pethome.dept.mapper.IDepartmentMapper;
import cn.itsource.pethome.dept.query.DepartmentQuery;
import cn.itsource.pethome.dept.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service//一定要
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private IDepartmentMapper departmentMapper;

    @Transactional
    @Override
    public void add(Department department) {//重写
        //先保存自己  为了去获取自己的id
        departmentMapper.add(department);
        this.update(department);//你特有的
    }

    @Transactional
    @Override
    public void update(Department department) {//重写
        String dirPath = "";
        if(department.getParent() != null){//如果为null就是顶级
            //获取上级的dirPath
            Long id = department.getParent().getId();//获取上级id
            Department parenDept = this.findById(id);//从数据库里面查询出来的
            dirPath = parenDept.getDirPath();//从数据库查询出来的
        }
        //你有没有自己的id  肯定没有  获取自己的id
        Long id = department.getId();
        dirPath = dirPath + "/" + id;//上级路径加上自己
        department.setDirPath(dirPath);
        departmentMapper.update(department);//修改
    }

    @Override
    public List<Department> getTree() {
       /* //第一步查询所有部门
        List<Department> deptAll = departmentMapper.findAll();
        //专门用装顶级部门的
        List<Department> firstDept = new ArrayList<>();
        for (Department department : deptAll) {
            //判断parent_id == null 就是顶级部门
            if(department.getParent_id() == null){
                firstDept.add(department);
            }else{
                //不是顶级部门
                for (Department parentDept : deptAll) {
                    //找自己的爹
                    if(department.getParent_id() == parentDept.getId()){
                        //把自己放入到爹里面的children
                        parentDept.getChildren().add(department);
                    }
                }
            }
        }*/
        return getTree2();
    }

    public List<Department> getTree2(){
        //第一步查询所有部门
        List<Department> deptAll = departmentMapper.findAll();
        //全是父级
        Map<Long,Department> parentDept = new HashMap<>();
        for (Department department : deptAll) {
            parentDept.put(department.getId(),department);
        }
        //专门用装顶级部门的
        List<Department> firstDept = new ArrayList<>();
        for (Department department : deptAll) {
            if(department.getParent_id() == null){
                firstDept.add(department);
            }else{
                Long parent_id = department.getParent_id();
                Department pDept = parentDept.get(parent_id);
                pDept.getChildren().add(department);
            }
        }
        return firstDept;
    }

}
